/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pojos.DayDate;
import pojos.Employees;
import pojos.TimeLog;
import pojos.TimeLogViewBean;

/**
 *
 * @author A7med Kotb
 */
@Stateless
public class TimeLogFacade extends AbstractFacade<TimeLog> {

    @PersistenceContext(unitName = "AquantimaISPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public TimeLogFacade() {
        super(TimeLog.class);
    }

    public List getUserTimeLog(Employees employee) {
        System.out.println("##########Employee ID: " + employee.getEmpId());
        Query query = getEntityManager().createNamedQuery("TimeLog.findByEmployee");
        query.setParameter("emplyee", employee);
        List mylist = query.getResultList();
        return mylist;
    }

    public List getNestedTimeLog(Date weekDay, Employees employee) {

        Calendar call = Calendar.getInstance();
        call.setTime(weekDay);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        List<DayDate> dayDateList = new ArrayList<DayDate>();

        Calendar cal = (Calendar) call.clone();
        cal.add(Calendar.DAY_OF_WEEK, (cal.getFirstDayOfWeek() - cal.get(Calendar.DAY_OF_WEEK)));

        df.format(cal.getTime());

        DayDate firstElement = new DayDate();
        firstElement.setDayName("Day 1");
        firstElement.setDayDate(cal.getTime());
        System.out.println("##############Day 1: " + firstElement.getDayDate());

        dayDateList.add(firstElement);

        for (int i = 1; i < 7; i++) {

            Calendar day = (Calendar) cal.clone();
            day.add(Calendar.DAY_OF_YEAR, i);

            df.format(day.getTime());
            int count = i + 1;
            DayDate curr = new DayDate();
            curr.setDayName("Day " + count);
            curr.setDayDate(day.getTime());

            System.out.println("##############Day " + i + 1 + ": " + firstElement.getDayDate());

            dayDateList.add(curr);
        }
        System.out.println("#################List Length: " + dayDateList.size());
        List<TimeLogViewBean> tlvbList = new ArrayList<TimeLogViewBean>();
        for (int i = 0; i < dayDateList.size(); i++) {
            String currDay = dayDateList.get(i).getDayName();
            Date currDate = dayDateList.get(i).getDayDate();
            Query query = getEntityManager().createNamedQuery("TimeLog.findNestedTimeLogs");
            query.setParameter("emplyee", employee);
            query.setParameter("date", currDate);
            List<TimeLog> timlogList = query.getResultList();
            TimeLogViewBean tlvb = new TimeLogViewBean();
            tlvb.setDay(currDay);
            tlvb.setDate(currDate);
            tlvb.setTimlog(timlogList);
            int dayHours = 0;
            System.out.println("############TimeLog List Size: " + timlogList.size());
            if (timlogList.size() > 0) {
                for (int cc = 0; cc<timlogList.size(); cc++) {
                    dayHours += timlogList.get(cc).getWorkedHours();
                }
            }
            tlvb.setDayHours(dayHours);
            tlvbList.add(tlvb);
        }
        System.out.println("#################tlvbList Length: " + tlvbList.size());
        return tlvbList;
    }

    public TimeLog getTL(int tlId) {
        Query query = getEntityManager().createNamedQuery("TimeLog.findByTlId");
        query.setParameter("tlId", tlId);
        return  (TimeLog)query.getSingleResult();
    }
}
